<?php
	include("datos.php");
	//session_name('usuario');
	//session_start();
	//$id_user=base64_encode($_SESSION['id_empleado']);
	//if($_SESSION['usuario'] == '' || $_SESSION['id_empleado'] == '')
	//	header("location:login.php?error=1");

	// Get the requested page. By default grid sets this to 1.
	$page = $_GET['page'];
	// get how many rows we want to have into the grid - rowNum parameter in the grid
	$limit = $_GET['rows'];
	// get index row - i.e. user click to sort. At first time sortname parameter -
	// after that the index from colModel
	$sidx = $_GET['sidx'];
	// sorting order - at first time sortorder
	$sord = $_GET['sord'];
	// if we not pass at first time index use the first column for the index or what you want
	if(!$sidx) $sidx =1;
		
	
	//****PARA BUSQUEDA PERSONALIZADA (autocomplete)*****//
	/*if(isset($_GET["nm_mask"]))
		$nm_mask = $_GET['nm_mask'];
	else $nm_mask = "";*/
	if(isset($_GET["cd_mask"]))
		$cd_mask = $_GET['cd_mask'];
		else $cd_mask = "";
	//construct where clause 
	$where = "";
	/*if($nm_mask!='')
		$where.= " AND Nombre LIKE '$nm_mask%'";*/
	if($cd_mask!='')
//		$where = "";
		$where.= " AND descripcion LIKE '%$cd_mask%'";


		
	// the actual query for the grid data
	$consulta="SELECT * FROM tipocaprino WHERE idtipocaprino <> 0 ";
	
	// calculate the number of rows for the query. We need this for paging the result
	$count=$mi_objeto->contar($consulta);
	
	// calculate the total pages for the query
	if( $count > 0 && $limit > 0) {
		$total_pages = ceil($count/$limit);
	} else {
		$total_pages = 0;
	}
	
	// if for some reasons the requested page is greater than the total
	// set the requested page to total page
	if ($page > $total_pages) $page=$total_pages;
	
	// calculate the starting position of the rows
	$start = $limit*$page - $limit;
	
	// if for some reasons start position is negative set it to 0
	// typical case is that the user type 0 for the requested page
	if($start <0) $start = 0;

	$consulta="SELECT * FROM tipocaprino WHERE idtipocaprino <> 0 ".$where." ORDER BY $sidx $sord LIMIT $start , $limit";
	
	$result=$mi_objeto->ejecutar_consulta($consulta);
	// we should set the appropriate header information. Do not forget this.
	//header("Content-type: text/xml;charset=utf-8");
	
	
	$responce->page = $page;
	$responce->total = $total_pages;
	$responce->records = $count;
	$i=0;
	while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
		$responce->rows[$i]['id']=$row[idtipocaprino];
		$responce->rows[$i]['cell']=array("",$row[descripcion]);
		$i++;
	}
	echo json_encode($responce);

/*	
	$s = "<?xml version='1.0' encoding='utf-8'?>";
	$s .=  "<rows>";
	$s .= "<page>".$page."</page>";
	$s .= "<total>".$total_pages."</total>";
	$s .= "<records>".$count."</records>";
	 
	 be sure to put text data in CDATA
	while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
		$s .= "<row id='". $row['id_paciente']."'>";
		$s .= "<cell>". $row['id_paciente']."</cell>";
		$s .= "<cell><![CDATA[". $row['Apellido']."]]></cell>";
		$s .= "<cell><![CDATA[". $row['Nombre']."]]></cell>";
		$s .= "<cell><![CDATA[". $row['Tipo']."]]></cell>";
		$s .= "<cell>". $row['Numero']."</cell>";
		$s .= "<cell><![CDATA[". $row['Domicilio']."]]></cell>";
		$s .= "<cell><![CDATA[". $row['Telefono']."]]></cell>";
		$s .= "<cell><![CDATA[". $row['Descripcion']."]]></cell>";
		$s .= "<cell><![CDATA[". $row['NumeroAfiliado']."]]></cell>";
		$s .= "</row>";
	}
	$s .= "</rows>";
	
	echo $s;
*/
?>